package main

import (
	"fmt"
	"sync"
)

var (
	counter  int
	mu       sync.Mutex
	counter2 int
)

func increment(wg *sync.WaitGroup) {
	defer wg.Done()
	mu.Lock()
	defer mu.Unlock()
	counter++
}

func increment2(wg *sync.WaitGroup) {
	defer wg.Done()
	counter2++
}

func main() {
	var wg2 sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg2.Add(1)
		go increment2(&wg2)
	}

	wg2.Wait()
	// 这里的 counter2 没有加锁，所以可能会出现数据竞争问题

	// Final Counter2: 99
	fmt.Println("Final Counter2:", counter2)

	var wg sync.WaitGroup
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go increment(&wg)
	}
	wg.Wait()

	// Final Counter: 10
	fmt.Println("Final Counter:", counter)
}
